package com.otoe.dilidilivid.data.dao;

import com.otoe.dilidilivid.data.entity.Video;
import com.otoe.dilidilivid.data.entity.VideoInfo;
import com.otoe.dilidilivid.data.operation.MysqlOperation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VideoDao {
    private static final String TAG = "mysql-dilidiliDB-UserDao";
    //获得链接
    Connection connection;
    {
        try {
            connection = MysqlOperation.DBUtils.getConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //查询所有视频信息
    public List<VideoInfo> selectAll() {
        String sql = "select * from VIDEOS";
        List<VideoInfo> videos = new ArrayList<>();
        if (connection != null) {
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null) {
                    // 执行sql查询语句并返回结果集
                    ResultSet rs = ps.executeQuery();
                    while (rs.next()) {
                        VideoInfo video = new VideoInfo();
                        video.setVid(rs.getInt(1));
                        video.setTitle(rs.getString(2));
                        video.setUname(rs.getString(3));
                        video.setVideoUrl(rs.getString(4));
                        video.setTags(rs.getString(5));
                        video.setPlayNums(rs.getInt(6));
                        video.setImgUrl(rs.getInt(7));
                        video.setDuration(rs.getString(8));
                        video.setImg(rs.getString(9));
                        videos.add(video);
                    }
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return videos;
    }

    //设置视频时长
    public void changeDuration() {
        String sql = "update VIDEOS set duration=? where V_id=?";
        if (connection != null) {
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null) {
                    for (int i = 1; i <= 20; i++) {
                        ps.setString(1,(int)(Math.random()*10)+":"+(int)(Math.random()*60));
                        ps.setInt(2,i);
                        ps.addBatch();
                    }
                    ps.executeBatch();
                    ps.clearBatch();
                    ps.close();
                }
            } catch (SQLException e) {

            }

        }
    }
    //播放次数加1
    public void addPlayTime(VideoInfo videoInfo){
        String sql="update VIDEOS set playNums=? where V_id=?";
        if(connection!=null){
            try {
                PreparedStatement ps=connection.prepareStatement(sql);
                if(ps!=null){
                    ps.setInt(1,videoInfo.getPlayNums()+1);
                    ps.setInt(2,videoInfo.getVid());
                    ps.execute();
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //插入视频
    public void insertVideo(VideoInfo videoInfo){
        String sql="insert into VIDEOS(V_title,Uname,VideoUrl,Tags,playNums,ImageUrl,duration,Img) values(?,?,?,?,?,?,?,?)";
        if(connection!=null){
            try {
                PreparedStatement ps=connection.prepareStatement(sql);
                if(ps!=null){
                    ps.setString(1,videoInfo.getTitle());
                    ps.setString(2,videoInfo.getUname());
                    ps.setString(3,videoInfo.getVideoUrl());
                    ps.setString(4,videoInfo.getTags());
                    ps.setInt(5,videoInfo.getPlayNums());
                    ps.setInt(6,videoInfo.getImgUrl());
                    ps.setString(7,videoInfo.getDuration());
                    ps.setString(8,videoInfo.getImg());

                    ps.execute();
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
}
